package com.qk.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.management.annotation.LogAnno;
import com.qk.result.PageResult;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:zhengbowen
 * @Date: 2025/10/06 18:44
 * @Description: 角色管理业务逻辑
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {

    private final RoleMapper roleMapper;

    @Override
    public PageResult<Role> selectRolesByPage(RoleDTO roleDto) {
        PageHelper.startPage(roleDto.getPage(), roleDto.getPageSize());
        List<Role> roleList = roleMapper.selectRolesByPage(roleDto);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        return PageResult.<Role>builder()
                .total(pageInfo.getTotal())
                .rows(pageInfo.getList())
                .build();
    }



    @Override
    @LogAnno
    public void updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }

    @Override
    public Role selectRoleById(Integer id) {
        return roleMapper.selectRoleById(id);
    }

    @Override
    public List<Role> selectRoles() {
        return roleMapper.selectRoles();
    }

    @Override
    @LogAnno
    public void insertRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insertRole(role);
    }

    @Override
    @LogAnno
    public void deleteRole(Integer id) {
        roleMapper.deleteRole(id);
    }
}
